package edu.eas.admin.controller;

import edu.eas.admin.service.DictDataService;
import edu.eas.admin.service.DictTypeService;
import edu.eas.common.api.CommonPage;
import edu.eas.common.api.CommonResult;
import edu.eas.common.dto.DictDataParam;
import edu.eas.common.pojo.DictData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cql
 */
@RestController
@Api(tags = "DictDataController", description = "字典数据管理")
@RequestMapping("/dict/data")
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private DictTypeService dictTypeService;


    @ApiOperation(value = "获取字典数据详情")
    @GetMapping("{dictCode}")
    public CommonResult<DictData> getInfo(@PathVariable("dictCode") Long dictCode) {
        DictData dictData = dictDataService.getByDictCode(dictCode);
        return CommonResult.success(dictData);
    }


    @ApiOperation(value = "新增字典数据")
    @PostMapping
    public CommonResult create(@Validated @RequestBody DictData dict) {
        // TODO 先判断存不存在相同的字典数据
        int count = dictDataService.create(dict);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "修改字典数据")
    @PutMapping("{dictCode}")
    public CommonResult update(@PathVariable("dictCode") Long dictCode, @RequestBody DictData dictData) {
        dictData.setDictCode(dictCode);
        int count = dictDataService.update(dictData);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "移除字典数据(支持批量移除)")
    @DeleteMapping("/{dictCodes}")
    public CommonResult delete(@PathVariable Long[] dictCodes) {
        int count = dictDataService.delete(dictCodes);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation(value = "分页查询字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = false),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false),
            @ApiImplicitParam(name = "dictLabel", value = "字典名称", required = false),
            @ApiImplicitParam(name = "dictType", value = "字典类型", required = false),
            @ApiImplicitParam(name = "status", value = "启用状态：0:禁用; 1:启用", required = false),
    })
    @GetMapping("/list")
    public CommonResult<CommonPage<DictData>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "dictLabel", defaultValue = "") String dictLabel,
                                                   @RequestParam(value = "dictType", defaultValue = "") String dictType,
                                                   @RequestParam(value = "status", defaultValue = "-1") Integer status) {
        List<DictData> menuList = dictDataService.list(pageSize, pageNum, dictLabel, dictType, status);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation(value = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public CommonResult<List<DictData>> dictType(@PathVariable String dictType) {
        List<DictData> data = dictTypeService.selectDictDataByType(dictType);
        return CommonResult.success(data);
    }

}
